package com.kolon.business.main.controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.main.dao.MainDao;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.MsgUtil;
import com.kolon.framework.util.XmlUtil;

@Controller("com.kolon.business.main.controller.XmlBannerController")
public class XmlBannerController implements SimpleController {
	
	@Resource(name="MainDaoImpl")
	private MainDao mainDao;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/main/xmlBanner.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		XmlUtil xml = new XmlUtil();
		Document doc = xml.make("BNER_LIST");
		// PATH 정보
		String bnerUrl = MsgUtil.getMessage("service.banner.url");
		String domainUrl = MsgUtil.getMessage("domain.url");
		// 배너 목록 정보 조회
		List bnerList = mainDao.getXmlBnerList(null);
		// 엘리먼트 생성
		LinkedHashMap<String, Object> dataList = new LinkedHashMap<String, Object>();
		for (int i=0, size=bnerList.size(); i<size; i++) {
			LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
			Map map = (Map)bnerList.get(i);
			data.put("BNER_NO", map.get("BNER_NO"));
			data.put("BNER_NM", map.get("BNER_NM"));
			data.put("IMG_NM", (map.get("IMG_NM") != null) ? domainUrl + bnerUrl + map.get("IMG_NM") : "");
			data.put("LNK_URL", (map.get("LNK_URL") != null) ? map.get("LNK_URL") : "");
			dataList.put("BNER_INFO", data);
		}
		xml.addElement(doc.getRootElement(), dataList);
		Map model = new HashMap();
		model.put("xmlDoc", doc);
		// 리턴
		return new ModelAndView("xmlView", model);
	}

}
